package org.dromara.wms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.bo.WmsStockLogBo;
import org.dromara.wms.domain.vo.WmsStockLogVo;
import org.dromara.wms.domain.WmsStockLog;
import org.dromara.wms.mapper.WmsStockLogMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 出入库记录Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WmsStockLogServiceImpl extends BaseService {

    private final WmsStockLogMapper baseMapper;

    /**
     * 查询出入库记录
     */
    public WmsStockLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询出入库记录列表
     */
    public TableDataInfo<WmsStockLogVo> queryPageList(WmsStockLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WmsStockLog> lqw = buildQueryWrapper(bo);
        Page<WmsStockLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出入库记录列表
     */
    public List<WmsStockLogVo> queryList(WmsStockLogBo bo) {
        LambdaQueryWrapper<WmsStockLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WmsStockLog> buildQueryWrapper(WmsStockLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WmsStockLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, WmsStockLog::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getMeasureUnit()), WmsStockLog::getMeasureUnit, bo.getMeasureUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchCode()), WmsStockLog::getBatchCode, bo.getBatchCode());
        lqw.eq(bo.getOrderId() != null, WmsStockLog::getOrderId, bo.getOrderId());
        lqw.eq(bo.getPrice() != null, WmsStockLog::getPrice, bo.getPrice());
        lqw.eq(bo.getWarehouseId() != null, WmsStockLog::getWarehouseId, bo.getWarehouseId());
        lqw.eq(bo.getAreaId() != null, WmsStockLog::getAreaId, bo.getAreaId());
        lqw.eq(bo.getQuantityBefore() != null, WmsStockLog::getQuantityBefore, bo.getQuantityBefore());
        lqw.eq(bo.getQuantity() != null, WmsStockLog::getQuantity, bo.getQuantity());
        return lqw;
    }

    /**
     * 新增出入库记录
     */
    public Boolean insertByBo(WmsStockLogBo bo) {
        WmsStockLog add = MapstructUtils.convert(bo, WmsStockLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改出入库记录
     */
    public Boolean updateByBo(WmsStockLogBo bo) {
        WmsStockLog update = MapstructUtils.convert(bo, WmsStockLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WmsStockLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出入库记录
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
